# encoding: utf-8

def listfiles (data, cmd):
  import os, os.path
  if os.path.exists(data.filelist.path):
    data.filelist.files = os.listdir(data.filelist.path)
  else:
    data.filelist.files = []
  data.filelist.command = cmd
  data.updateFileList()

def changedir (data, cmd):
  import os, os.path
  path = cmd[3:]
  if path[-1] != os.sep: path += os.sep
  print path
  path = os.path.join(data.filelist.path, path)
  path = os.path.normpath(path)
  if os.path.exists(path):
    data.filelist.path = path
    listfiles(data, 'ls')
  else:
    print 'No such path: ' + path

def quit(lish, cmd):
  lish.close()

def run_default(lish, index):
  import os, os.path
  file = lish.filelist.path
  if file[-1] != os.sep: file += os.sep
  file += lish.filelist.files[index]
  if os.path.isdir(file):
    changedir(lish, 'cd ' + lish.filelist.files[index])
  else:
    print file, 'is not directory'

